function DoubleLinkList() {
  this.length = 0;
  this.head = null;
  this.end = null;
  function Node(data) {
    this.prev = null;
    this.next = null;
    this.data = data;
  }
  let p = DoubleLinkList.prototype;
  p.append = function(data) {
    let node = new Node(data);

    if (this.head === null) {
      this.head = node;
      this.end = node;
    } else {
      let current = this.head;
      while (current.next) {
        current = current.next;
      }
      current.next = node;
      node.prev = current;
      this.end = node;
    }
  };
  p.insert = function(position, data) {
    let node = new Node(data);
    let i = 0;
    let front = null;
    let current = this.head;
    let flag = true;
    while (current) {
      if (position == i) {
        front.next = node;
        node.prev = front;
        node.next = current;
        current.prev = node;
        this.length++;
        flag = false;
        return;
      }
      front = current;
      current = current.next;
      i++;
    }
    if (flag) {
      throw new Error("insert未找到：下标为" + position + "的项");
    }
  };
  p.get = function(position) {
    let current = this.head;
    let i = 0;
    let flag = true;
    while (current) {
      if (position == i) {
        flag = false;
        return current;
      }
      current = current.next;
      i++;
    }
    if (flag) {
      throw new Error("get未找到：下标为" + position + "的项");
    }
  };
  p.indexOf = function(data) {
    let current = this.head;
    let i = 0;
    let flag = true;
    while (current) {
      if (data == current.data) {
        flag = false;
        return current;
      }
      current = current.next;
      i++;
    }
    if (flag) {
      return -1;
    }
  };
  p.update = function(position, data) {
    let current = this.head;
    let i = 0;
    let flag = true;
    while (current) {
      if (position == i) {
        current.data = data;
        flag = false;
        return;
      }
      current = current.next;
      i++;
    }
    if (flag) {
      throw new Error("update未找到：下标为" + position + "的项");
    }
  };
  p.removeAt = function(position) {
    let front = null;
    let current = this.head;
    let i = 0;
    let flag = true;
    while (current) {
      if (position == i) {
        if (front && front.next) {
          front.next = current.next;
        } else {
          this.head = current.next;
        }
        if (current.next) {
          current.next.prev = front;
        } else {
          this.end = front;
        }
        flag = false;
        this.length--;
        return;
      }
      front = current;
      current = current.next;
      i++;
    }
    if (flag) {
      throw new Error("removeAt未找到：下标为" + position + "的项");
    }
  };
  p.remove = function(data) {
    let front = null;
    let current = this.head;
    let flag = true;
    while (current) {
      if (current.data === data) {
        console.log(data);
        if (front == null) {
          // 说明这是第一个
          this.head = current.next;
          current.next.prev = null;
        } else if (current.next == null) {
          // 最后一个
          this.end = front;
          front.next = null;
        } else {
          // 中间
          front.next = current.next;
          current.next.prev = front;
        }
        this.length--;
        flag = false;
        return;
      }
      front = current;
      current = current.next;
    }
    if (flag) {
      throw new Error("reomve 方法 未找到 值为", data, "的项");
    }
  };
  p.isEmpty = function() {
    return this.head === null;
  };
  p.size = function() {
    let i = 0;
    let current = this.head;
    while (current) {
      current = current.next;
      i++;
    }
    return i;
  };
  p.toString = function() {
    let current = this.head;
    let str = "";
    while (current) {
      str += current.data;
      current = current.next;
    }
    console.log(this.head);
    return str;
  };
}
var dll = new DoubleLinkList();
dll.append(1);
dll.append(2);
dll.append(3);
dll.append(4);
dll.append(5);
console.log(dll.toString());

dll.remove(5);
console.log(dll.toString());
dll.remove(1);
console.log(dll.toString());
dll.remove(3);
console.log(dll.toString());
